package com.zzcm.channelinfo.appmanager;

import android.content.Context;
import android.content.Intent;
import java.io.File;
import java.util.ArrayList;
import com.zzcm.channelinfo.control.SilentInstallationControl;
import com.zzcm.channelinfo.tools.Tools;

/**
 * 应用静默安装、静默卸载管理类
 * @author 张少志
 *当前代码已经相对稳定
 */
public class AppManager {
    private Context context;
    private ArrayList<AppInfo> installAppList;//安装应用路径列表
    private ArrayList<AppInfo> unInstallAppList;//卸载应用包名列表
    public AppManager(Context context) {
        this.context = context;
        installAppList = new ArrayList<AppInfo>();
        unInstallAppList = new ArrayList<AppInfo>();
    }
    
    public void setContext(Context context) {
        this.context = context;
    }
    
    /**
     * 显示LOG信息
     */
    private String TAG = this.getClass().getName();
    private boolean showLog = true;// true;//false;
    public void showLog(String tag, String msg) {
        if (showLog) {
            Tools.showLog(TAG,tag+" : "+msg);
        }
    }
    
    /**
     * 卸载当前资源
     */
    public void appManagerUnInit() {
        stopAllThread();
        installAppList.clear();
        unInstallAppList.clear();
        installAppList = null;
        unInstallAppList = null;
        this.context = null;
    }
    
    /**
     * 停止所有正在运行的线程
     */
    private void stopAllThread() {
        stopInstallAppThread();
        stopUninstallAppThread();
        stopCheckInstallTimeOutThread();
        stopCheckUninstallTimeOutThread();
        try {
            Thread.sleep(20);
        } catch(InterruptedException e) {
            
        }
    }
    
    /**
     * 添加应用安装路径
     * @param appPath
     * @return
     */
    private boolean addInstallApp(String appPath,boolean root,boolean delete,int location) {
        if(appPath==null||appPath.trim().equals(""))
        {
            return false;
        }
        AppInfo tempApp;
        String tempPath;
        for(int i=0;i<installAppList.size();i++)
        {
            tempApp = installAppList.get(i);
            if(tempApp!=null)
            {
            	tempPath=tempApp.getAppUniqueKey();
	            if(tempPath!=null&&tempPath.equals(appPath))
	            {
	                return false;
	            }
            }
        }
        installAppList.add(new AppInfo(appPath,root,delete,location));
        return true;
    }
    

    /**
     * 添加卸载应用包名
     * @param appPackageName
     * @return
     */
    private boolean addUninstallApp(String appPackageName,boolean root) {
        if(appPackageName==null||appPackageName.trim().equals(""))
        {
            return false;
        }
        AppInfo tempApp;
        String tempPackage;
        for(int i=0;i<unInstallAppList.size();i++)
        {
            tempApp = unInstallAppList.get(i);
            if(tempApp!=null)
            {
            	tempPackage=tempApp.getAppUniqueKey();
	            if(tempPackage!=null&&tempPackage.equals(appPackageName))
	            {
	                return false;
	            }
            }
        }
        unInstallAppList.add(new AppInfo(appPackageName,root));
        return true;
    }
    

    /**
     * 停止安装应用列表
     */
    private void stopInstallAppThread() {
        if(installAppThread!=null)
        {
            try {
                installAppThread.destroy();
            } catch (NullPointerException e) {

            }
            installAppThread = null;
        }
        currentInstallPath=null;
    }
    
    /**
     * 停止检查当前软件安装是否超时
     */
    private void stopCheckInstallTimeOutThread() {
        if(checkInstallTimeOutThread!=null)
        {
            try {
            	checkInstallTimeOutThread.destroy();
            } catch (NullPointerException e) {

            }
            checkInstallTimeOutThread = null;
        }
    }
    
    private Thread checkInstallTimeOutThread;
    private int beginInstall,timeOutSecondInstall=60;
    private String currentInstallPath;
    private void startInstallTimeOutCheckThread() {
    	if(checkInstallTimeOutThread==null)
    	{
    		checkInstallTimeOutThread=new Thread() {
    			//线程是否需要停止的判断标志，默认为false
                private final boolean stopThread[] = {
                        false
                };
                //用于反馈当前线程的运行状态
                @Override
                public boolean isInterrupted() {
                    return stopThread[0];
                }
                //停止当前运行的线程
                @Override
                public void destroy() {
                    stopThread[0] = true;
                }

                @Override
                public void run() {
                	SilentInstallationControl mSAControl = SilentInstallationControl.getInstence();
                	mSAControl.startNewThread();
                	String appPath;
                	while(!stopThread[0]&&currentInstallPath!=null)
                	{
                		while(!stopThread[0]&&beginInstall<timeOutSecondInstall)
                		{
                			try {
                				Thread.sleep(1000);//每秒轮询一次检测是否超时
                            } catch (InterruptedException e1) {
                            }
                			beginInstall++;
                		}
                		if(!stopThread[0]&&currentInstallPath!=null)
                    	{
                			Intent fail = new Intent(SilentInstallationControl.AppManagerParameter.INSTALL_APP_FAIL);//广播通知安装失败
                            fail.putExtra(SilentInstallationControl.AppManagerParameter.APP_PATH, currentInstallPath);
                            fail.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, SilentInstallationControl.AppManagerParameter.INSTALL_TIMEOUT_RESULT);//安装超时
                            if(context!=null)
                            {
                                context.sendBroadcast(fail);
                            }
                            appPath=currentInstallPath;
                            currentInstallPath=null;
                            removeInstallApp(appPath);
                    	}
                	}
                	if(!stopThread[0])
                	{
                		checkInstallTimeOutThread=null;
	                    stopThread[0] = true;
                	}
                	mSAControl.destroyOldThread();
                }
    		};
    		checkInstallTimeOutThread.start();
    	}
    }
    
    /**
     * 线程安装应用列表
     */
    private Thread installAppThread;
    private void runInstallAppThread() {
        if(installAppThread==null)
        {
            installAppThread = new Thread() {
                //线程是否需要停止的判断标志，默认为false
                private final boolean stopThread[] = {
                        false
                };
                //用于反馈当前线程的运行状态
                @Override
                public boolean isInterrupted() {
                    return stopThread[0];
                }
                //停止当前运行的线程
                @Override
                public void destroy() {
                    stopThread[0] = true;
                }

                @Override
                public void run() {
                	SilentInstallationControl mSAControl = SilentInstallationControl.getInstence();
                	mSAControl.startNewThread();
                	AppInfo appInfo;
                	File source;
                	File ftemp = context.getFileStreamPath(".install.apk");
                    String appPath,installResult="",installPath="";
                    boolean root = false,install=false,delete=false;
                    int location = 0;
                    String installLocation;
                    long length;
                    ArrayList<String> commandList = new ArrayList<String>();
                    commandList.add(null);
                	//outTimeSecondInstall=60;//设定单次安装超时时间为60秒
                    while(!stopThread[0]&&installAppList.size()>0)
                    {
                    	appInfo = installAppList.get(0);//每次取队列的第一个路径进行安装
                    	if(!stopThread[0]&&appInfo!=null)
                    	{
                    		appPath = appInfo.getAppUniqueKey();
                    		root = appInfo.isRoot();
                    		delete = appInfo.isDelete();
                    		location = appInfo.getLocation();
                    		if(!stopThread[0]&&!root&&Tools.isSdcardPath(appPath))//使用系统权限，安装SD卡上的APK，不能做文件检测，因为系统权限不能访问SD卡，android的安全性机制。
                    		{
                    			install=true;
                    			installPath = appPath;
                    		}
                    		else
                    		{
		                        if(!stopThread[0]&&appPath!=null&&!appPath.trim().equals(""))
		                        {
		                        	install=false;
		                        	source = new File(appPath);
	                        		if(!stopThread[0])//原路径存在且可读        //&&Tools.isFileExistsAndCanRead(source)  系统签名后无访问SD卡权限，不做路径检测较妥当
	                        		{
	                        			Tools.deleteFile(ftemp);//new File(SilentInstallationControl.AppManagerParameter.APPLICATION_PATH+"/files/.install.apk")
	                        			Tools.deleteFile(new File(SilentInstallationControl.AppManagerParameter.SDCARD_PATH+"/.install.apk"));
	                        			if(!stopThread[0]&&Tools.isSdcardPath(appPath))//应用在SD卡里
	                        			{
		                        			length=source.length();
		                        			if(!stopThread[0]&&Tools.getPhoneAvailableSize(context)>(3*length))//手机中的空间足够大
		                        			{
		                        				installPath = ftemp.getAbsolutePath();//SilentInstallationControl.AppManagerParameter.APPLICATION_PATH+"/files/.install.apk"
		                        				if(!stopThread[0]&&Tools.copyFileToPhone(context, appPath, ".install.apk",Context.MODE_WORLD_READABLE))
		                        				{
		                        					install=true;
		                        				}
		                        				else
		                        				{
		                        					Tools.deleteFile(new File(installPath));
		                        				}
		                        			}
		                        			if(!stopThread[0]&&!install)
		                        			{
		                        				install=true;
		                        				if(!stopThread[0]&&Tools.getSDCardAvailableSize()>(15*length/10))//SD卡中的空间足够大
			                        			{
		                        					installPath = SilentInstallationControl.AppManagerParameter.SDCARD_PATH+"/.install.apk";
			                        				if(!stopThread[0]&&!Tools.copyFileToFile(appPath, installPath))//拷贝失败，安装原路径
			                        				{
			                        					Tools.deleteFile(new File(installPath));
			                        					installPath = appPath;
			                        				}
			                        			}
		                        				else
		                        				{
		                        					installPath = appPath;
		                        				}
		                        			}
	                        			}
	                        			else
	                        			{
	                        				install=true;
	                        				if(!stopThread[0]&&Tools.hasChineseCharacter(appPath))//路径中包含汉字
	                        				{
	                        					length=source.length();
			                        			if(!stopThread[0]&&Tools.getPhoneAvailableSize(context)>(3*length))//手机空间足够，将包含汉字的软件复制后安装
			                        			{
			                        				installPath = ftemp.getAbsolutePath();//SilentInstallationControl.AppManagerParameter.APPLICATION_PATH+"/files/.install.apk"
			                        				if(!stopThread[0]&&!Tools.copyFileToPhone(context, appPath, ".install.apk",Context.MODE_WORLD_READABLE))//拷贝失败安装原路径
			                        				{
			                        					Tools.deleteFile(new File(installPath));
			                        					installPath = appPath;
			                        				}
			                        			}
			                        			else//手机空间不足，直接按原路径安装
			                        			{
		                        					installPath = appPath;
			                        			}
	                        				}
	                        				else//无汉字，直接按原路径安装
	                        				{
		                        				installPath = appPath;
	                        				}
	                        			}
	                        		}
	                        		else//文件路径不存在，直接安装失败
	                        		{
	                        			install=false;
	                        			installResult=SilentInstallationControl.AppManagerParameter.INSTALL_APPPATH_NOTEXSISTS_RESULT;
	                        		}
	                        	}
                    		}
                        	if(!stopThread[0]&&install)
                        	{
                        		if(!stopThread[0])//&&((!root&&Tools.isSdcardPath(appPath))||Tools.isFileExists(new File(installPath)))   系统签名后无访问SD卡权限，不做路径检测较妥当
                        		{
                        			beginInstall=0;//标志新的软件安装开始
                        			currentInstallPath=appPath;
                        			startInstallTimeOutCheckThread();//开启软件安装超时检测
                        			installLocation = "";
                        			if(location==2)//安装到SD卡
                        			{
                        				installLocation = "-s ";
                        			}
                        			else if(location==1)//安装到手机内部存储
                        			{
                        				installLocation = "-f ";
                        			}
		                            showLog("runInstallAppThread", "installCommand="+SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+appPath);
		                            if(!stopThread[0]&&!root)
		                            {
		                            	commandList.set(0, SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+installPath);//组装安装应用的命令
		                            	installResult = Tools.runCommand(commandList,stopThread);
		                            	if(!stopThread[0]&&Tools.hasChineseCharacter(installPath)&&(installResult==null||!installResult.endsWith("Success")))
		                            	{
		                            		beginInstall=0;
		                            		commandList.set(0, SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+Tools.getISOString(installPath));//组装安装应用的命令
			                            	installResult = Tools.runCommand(commandList,stopThread);
		                            	}
		                            }
		                            else
		                            {
		                            	if(!stopThread[0]&&Tools.hasChineseCharacter(installPath))//ROOT安装以ISO-8859-1编码优先
		                            	{
		                            		commandList.set(0, SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+Tools.getISOString(installPath));//组装安装应用的命令
			                            	installResult = Tools.runRootCommand(commandList,stopThread);
			                            	if(installResult==null||!installResult.endsWith("Success"))
			                            	{
			                            		beginInstall=0;
			                            		commandList.set(0, SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+installPath);//组装安装应用的命令
				                            	installResult = Tools.runCommand(commandList,stopThread);
			                            	}
		                            	}
		                            	else
		                            	{
		                            		if(!stopThread[0])
		                            		{
		                            			commandList.set(0, SilentInstallationControl.AppManagerParameter.INSTALL_APP_COMMAND+installLocation+installPath);//组装安装应用的命令
		                            			installResult = Tools.runRootCommand(commandList,stopThread);
		                            		}
		                            	}
		                            }
		                            showLog("runInstallAppThread", "installResult="+installResult);
                        		}
                        		else
                        		{
                        			installResult=SilentInstallationControl.AppManagerParameter.INSTALL_APPPATH_NOTEXSISTS_RESULT;
                        		}
                        	}
                            if(!stopThread[0]&&installResult!=null&&installResult.endsWith("Success"))//安装成功 installResult.toLowerCase().indexOf("success")>=0
                            {
                                if(!stopThread[0])
                                {
                                    Intent sucess = new Intent(SilentInstallationControl.AppManagerParameter.INSTALL_APP_SUCCESS);//广播通知安装成功
                                    sucess.putExtra(SilentInstallationControl.AppManagerParameter.APP_PATH, appPath);
                                    sucess.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, installResult);
                                    if(context!=null)
                                    {
                                        context.sendBroadcast(sucess);
                                    }
                                    currentInstallPath=null;//当次安装结束
                                    if(delete)
                                    {
                                    	Tools.deleteFile(new File(appPath));
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else//安装失败
                            {
                                if(!stopThread[0])
                                {
                                    Intent fail = new Intent(SilentInstallationControl.AppManagerParameter.INSTALL_APP_FAIL);//广播通知安装失败
                                    fail.putExtra(SilentInstallationControl.AppManagerParameter.APP_PATH, appPath);
                                    fail.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, installResult);
                                    if(context!=null)
                                    {
                                        context.sendBroadcast(fail);
                                    }
                                    currentInstallPath=null;//当次安装结束
                                }
                                else
                                {
                                    break;
                                }
	                        }
	                    }
                        try {
                            if(!stopThread[0])
                            {
                            	try{
                            		installAppList.remove(0);
                            	} catch(IndexOutOfBoundsException e) {
                            	}
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch (IndexOutOfBoundsException e) {
                        }
                    }
                    if(!stopThread[0])
                    {
                    	Tools.deleteFile(ftemp);//new File(SilentInstallationControl.AppManagerParameter.APPLICATION_PATH+"/files/.install.apk")
            			Tools.deleteFile(new File(SilentInstallationControl.AppManagerParameter.SDCARD_PATH+"/.install.apk"));
	                    installAppThread=null;
	                    stopThread[0] = true;
                    }
                	mSAControl.destroyOldThread();
                }
            };
            installAppThread.start();
        }
    }
    
    /**
     * 停止卸载应用列表
     */
    private void stopUninstallAppThread() {
        if(unInstallAppThread!=null)
        {
            try {
                unInstallAppThread.destroy();
            } catch (NullPointerException e) {

            }
            unInstallAppThread = null;
        }
        currentUninstallPackageName=null;
    }
    
    /**
     * 停止检查当前软件卸载是否超时
     */
    private void stopCheckUninstallTimeOutThread() {
        if(checkUninstallTimeOutThread!=null)
        {
            try {
            	checkUninstallTimeOutThread.destroy();
            } catch (NullPointerException e) {

            }
            checkUninstallTimeOutThread = null;
        }
    }
    
    private Thread checkUninstallTimeOutThread;
    private int beginUninstall,timeOutSecondUninstall=60;
    private String currentUninstallPackageName;
    private void startUninstallTimeOutCheckThread() {
    	if(checkUninstallTimeOutThread==null)
    	{
    		checkUninstallTimeOutThread=new Thread() {
    			//线程是否需要停止的判断标志，默认为false
                private final boolean stopThread[] = {
                        false
                };
                //用于反馈当前线程的运行状态
                @Override
                public boolean isInterrupted() {
                    return stopThread[0];
                }
                //停止当前运行的线程
                @Override
                public void destroy() {
                    stopThread[0] = true;
                }

                @Override
                public void run() {
                	SilentInstallationControl mSAControl = SilentInstallationControl.getInstence();
                	mSAControl.startNewThread();
                	String appPackageName;
                	while(!stopThread[0]&&currentUninstallPackageName!=null)
                	{
                		while(!stopThread[0]&&beginUninstall<timeOutSecondUninstall)
                		{
                			try {
                				Thread.sleep(1000);//每秒轮询一次检测是否超时
                            } catch (InterruptedException e1) {
                            }
                			beginUninstall++;
                		}
                		if(!stopThread[0]&&currentUninstallPackageName!=null)
                    	{
                			Intent fail = new Intent(SilentInstallationControl.AppManagerParameter.UNINSTALL_APP_FAIL);//广播通知卸载失败
                            fail.putExtra(SilentInstallationControl.AppManagerParameter.APP_PACKAGE_NAME, currentUninstallPackageName);
                            fail.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, SilentInstallationControl.AppManagerParameter.UNINSTALL_TIMEOUT_RESULT);
                            if(context!=null)
                            {
                                context.sendBroadcast(fail);
                            }
                            appPackageName=currentUninstallPackageName;
                            currentUninstallPackageName=null;
                            removeUninstallApp(appPackageName);
                    	}
                	}
                	if(!stopThread[0])
                	{
                		checkUninstallTimeOutThread=null;
	                    stopThread[0] = true;
                	}
                	mSAControl.destroyOldThread();
                }
    		};
    		checkUninstallTimeOutThread.start();
    	}
    }
    
    /**
     * 线程卸载应用列表
     */
    private Thread unInstallAppThread;
    private void runUninstallAppThread() {
        if(unInstallAppThread==null)
        {
            unInstallAppThread = new Thread() {
                private final boolean stopThread[] = {
                        false
                };
                
                @Override
                public boolean isInterrupted() {
                    return stopThread[0];
                }

                @Override
                public void destroy() {
                    stopThread[0] = true;
                }

                @Override
                public void run() {
                	SilentInstallationControl mSAControl = SilentInstallationControl.getInstence();
                	mSAControl.startNewThread();
                	AppInfo appInfo;
                    String appPackageName,unInstallResult="";
                    boolean root = false;
                    ArrayList<String> commandList = new ArrayList<String>();
                    commandList.add(null);
                    while(!stopThread[0]&&unInstallAppList.size()>0)
                    {
                    	appInfo = unInstallAppList.get(0);
                    	if(!stopThread[0]&&appInfo!=null)
                    	{
                    		appPackageName=appInfo.getAppUniqueKey();
                    		root = appInfo.isRoot();
	                        if(!stopThread[0]&&appPackageName!=null&&!appPackageName.trim().equals(""))
	                        {
	                        	if(!stopThread[0]&&Tools.isAppInstalled(context, appPackageName))
	                        	{
	                        		beginUninstall=0;
	                        	    currentUninstallPackageName=appPackageName;
	                        	    startUninstallTimeOutCheckThread();
		                            commandList.set(0, SilentInstallationControl.AppManagerParameter.UNINSTALL_APP_COMMAND+appPackageName);
		                            showLog("runUninstallAppThread", "unInstallCommand="+SilentInstallationControl.AppManagerParameter.UNINSTALL_APP_COMMAND+appPackageName);
		                            if(!root)
		                            {
		                            	unInstallResult = Tools.runCommand(commandList,stopThread);
		                            }
		                            else
		                            {
		                            	unInstallResult = Tools.runRootCommand(commandList,stopThread);
		                            }
		                            showLog("runUninstallAppThread", "unInstallResult="+unInstallResult);
	                        	}
	                        	else
	                        	{
	                        		unInstallResult = SilentInstallationControl.AppManagerParameter.UNINSTALL_APPPACKAGENAME_NOTEXSISTS_RESULT;
	                        	}
	                            if(!stopThread[0]&&unInstallResult!=null&&unInstallResult.endsWith("Success"))//卸载成功 unInstallResult.toLowerCase().indexOf("success")>=0
	                            {
	                                if(!stopThread[0])
	                                {
	                                    Intent sucess = new Intent(SilentInstallationControl.AppManagerParameter.UNINSTALL_APP_SUCCESS);//广播通知卸载成功
	                                    sucess.putExtra(SilentInstallationControl.AppManagerParameter.APP_PACKAGE_NAME, appPackageName);
	                                    sucess.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, unInstallResult);
	                                    if(context!=null)
	                                    {
	                                        context.sendBroadcast(sucess);
	                                        currentUninstallPackageName=null;
	                                    }
	                                }
	                                else
	                                {
	                                    break;
	                                }
	                            }
	                            else//卸载失败
	                            {
	                                if(!stopThread[0])
	                                {
	                                    Intent fail = new Intent(SilentInstallationControl.AppManagerParameter.UNINSTALL_APP_FAIL);//广播通知卸载失败
	                                    fail.putExtra(SilentInstallationControl.AppManagerParameter.APP_PACKAGE_NAME, appPackageName);
	                                    fail.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, unInstallResult);
	                                    if(context!=null)
	                                    {
	                                        context.sendBroadcast(fail);
	                                        currentUninstallPackageName=null;
	                                    }
	                                }
	                                else
	                                {
	                                    break;
	                                }
	                            }
	                        }
                    	}
                        try {
                            if(!stopThread[0])
                            {
                            	try{
                            		unInstallAppList.remove(0);
                            	} catch(IndexOutOfBoundsException e) {
                            	}
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch (IndexOutOfBoundsException e) {
                        }
                    }
                    if(!stopThread[0])
                    {
                    	unInstallAppThread=null;
                    	stopThread[0] = true;
                    }
                	mSAControl.destroyOldThread();
                }
            };
            unInstallAppThread.start();
        }
    }
    
    /**
     * 对外安装应用的公有接口
     * @param appPath
     */
    public void installApp(String appPath,boolean root,boolean delete,int location) {
        if(addInstallApp(appPath,root,delete,location))
        {
            showLog("installApp", "appPath="+appPath);
            runInstallAppThread();
        }
    }
    
    /**
     * 中止所有安装软件
     * @return
     */
    public boolean removeAllInstallApp() {
    	if(installAppList.size()==0)
        {
            return false;
        }
    	stopInstallAppThread();
    	installAppList.clear();
    	return true;
    }
    
    /**
     * 移除指定路径的安装软件
     * @param appPath
     * @return
     */
    public boolean removeInstallApp(String appPath) {
    	if(appPath==null||appPath.trim().equals(""))
        {
            return false;
        }
    	AppInfo tempApp;
        String tempPath;
        for(int i=0;i<installAppList.size();i++)
        {
            tempApp = installAppList.get(i);
            if(tempApp!=null)
            {
            	tempPath=tempApp.getAppUniqueKey();
	            if(tempPath!=null&&tempPath.equals(appPath))
	            {
	            	if(i>0)
	            	{
	            		try{
	                		installAppList.remove(i);
	                	} catch(IndexOutOfBoundsException e) {
	                	}
	            	}
	            	else
	            	{
            			stopInstallAppThread();
            			try{
	                		installAppList.remove(0);
	                	} catch(IndexOutOfBoundsException e) {
	                	}
            			if(installAppList.size()>0)
            			{
            				runInstallAppThread();
            			}
	            	}
	                return true;
	            }
            }
        }
        return false;
    }
    
    /**
     * 对外卸载应用的公有接口
     * @param appPackageName
     */
    public void unInstallApp(String appPackageName,boolean root) {
        if(addUninstallApp(appPackageName,root))
        {
            showLog("unInstallApp", "appPackageName="+appPackageName);
            runUninstallAppThread();
        }
    }
    
    /**
     * 中止所有软件被卸载
     * @return
     */
    public boolean removeAllUninstallApp() {
    	if(unInstallAppList.size()==0)
        {
            return false;
        }
    	stopUninstallAppThread();
    	unInstallAppList.clear();
    	return true;
    }
    
    /**
     * 移除指定包名的软件被卸载
     * @param appPath
     * @return
     */
    public boolean removeUninstallApp(String appPackageName) {
    	if(appPackageName==null||appPackageName.trim().equals(""))
        {
            return false;
        }
    	AppInfo tempApp;
        String tempPackage;
        for(int i=0;i<unInstallAppList.size();i++)
        {
            tempApp = unInstallAppList.get(i);
            if(tempApp!=null)
            {
            	tempPackage=tempApp.getAppUniqueKey();
	            if(tempPackage!=null&&tempPackage.equals(appPackageName))
	            {
	            	if(i>0)
	            	{
	            		try{
	            			unInstallAppList.remove(i);
	                	} catch(IndexOutOfBoundsException e) {
	                	}
	            	}
	            	else
	            	{
            			stopUninstallAppThread();
            			try{
            				unInstallAppList.remove(0);
	                	} catch(IndexOutOfBoundsException e) {
	                	}
            			if(unInstallAppList.size()>0)
            			{
            				runUninstallAppThread();
            			}
	            	}
	                return true;
	            }
            }
        }
        return false;
    }
    
}
